﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HJ212_Protocol;
using SailHero.Soft.ESB.Communication.ClientAndServer.Client;
using SailHero.Soft.ESB.Communication.ClientAndServer.Communication.EndPoints.Tcp;
using SailHero.Soft.ESB.Communication.ClientAndServer.Communication.Messages;
using System.Configuration;
namespace HJT212_Protocol_Client
{
    public partial class ClientForm : Form
    {
        #region 构造函数
        public ClientForm()
        {
            InitializeComponent();
        }
        #endregion

        #region 属性字段
        private IClient client;
        #endregion

        #region 事件

        private void btnConn_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtBoxContent.Text = "";
                string _port = ConfigurationManager.AppSettings["SrvPort"];
                string ProtocolSrv = ConfigurationManager.AppSettings["ProtocolSrv"];
                client = ClientFactory.CreateClient(new ESBTcpEndPoint(ProtocolSrv, Convert.ToInt32(_port)));
                client.MessageReceived += Client_MessageReceived;
                client.WireProtocol = new MyWireProtocol(); //Set custom wire protocol!
                client.MN = ConfigurationManager.AppSettings["MN"];
                client.PW = ConfigurationManager.AppSettings["PW"];
                client.Disconnected += client_Disconnected;
                client.Connect();
                btnSend.Enabled = true;
                string msg = "已经正确连接了服务端\r\n";
                this.txtBoxContent.AppendText(msg);
                txtBoxContent.Select(0, msg.Length);
                txtBoxContent.SelectionColor = Color.Black;
                this.btnConn.Enabled = false;
            }
            catch (Exception ex)
            {
                //throw;
            }
        }

        private void client_Disconnected(object sender, EventArgs e)
        {
            try
            {
                string msg = "服务器强制踢出！\r\n";

                Delegate ddd = new Action<string>(delegate(string str)
                {
                    this.btnConn.Enabled = true;
                    this.txtBoxContent.AppendText(str);
                    txtBoxContent.Select(Math.Max(0, this.txtBoxContent.Text.Length - str.Length), str.Length);
                    txtBoxContent.SelectionColor = Color.Red;
                    txtBoxContent.ScrollToCaret();
                });
                this.BeginInvoke(ddd, msg);
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string[] str1 = new string[60];
                for (int i = 0; i < 60; i++)
                {
                    str1[i] = "1";
                }
                string[] str2 = new string[60];
                for (int i = 0; i < 60; i++)
                {
                    str2[i] = "2";
                }

                string str = this.txtMsgToSend.Text;
                TextMessage Tmsg = new TextMessage(str);
                Tmsg.FromMN = string.Format("{0}{1}", client.MN, "");
                Tmsg.To = string.Join("", str2);
                client.SendMessage(Tmsg);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void Client_MessageReceived(object sender, MessageEventArgs e)
        {
            //Client only accepts text messages            
            var message = e.Message as TextMessage;
            if (message == null)
            {
                return;
            }
            string msg = message.Text;

            Delegate ddd = new Action<string>(delegate(string str)
            {
                this.txtBoxContent.AppendText(str + "\r\n");
                txtBoxContent.Select(this.txtBoxContent.Text.Length - str.Length, str.Length);
                txtBoxContent.SelectionColor = Color.Red;
            });
            this.BeginInvoke(ddd, msg);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            try
            {
                client.Disconnect(); //Close connection to server
                client.Dispose();
                client = null;
                base.OnClosing(e);
            }
            catch (Exception ex)
            {
                
            }
        }

        #endregion

        #region 方法
        /// <summary>
        /// 以不同状态显示数据
        /// </summary>
        /// <param name="msg">显示的数据</param>
        /// <param name="type">状态表示，2 黑色 否则红色</param>
        private void ShowMsg(string msg, int type)
        {
            Delegate ddd = new Action<string>(delegate(string str)
            {
                this.txtBoxContent.AppendText(str);
                txtBoxContent.Select(Math.Max(0, this.txtBoxContent.Text.Length - str.Length), str.Length);
                txtBoxContent.SelectionColor = type == 2 ? Color.Black : Color.Red;
                txtBoxContent.ScrollToCaret();
            });
            this.BeginInvoke(ddd, msg);
        }
        #endregion
    }
}
